﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using w = Microsoft.Office.Interop.Word;

namespace z.v{
	/// <summary>
	/// Interaction logic for Main.xaml
	/// </summary>
	public partial class Main : Window{
		public Main(){
			SqlConnection mconn = new SqlConnection(Properties.Settings.Default.ConnectSting);
			try {
				mconn.Open();
				mconn.Close();
				App.DB = new m.DataClassesDataContext(mconn);
				App.W = new w.Application();
				App.Current.Exit += Current_Exit;
				InitializeComponent();
			} catch (Exception ex) {
				new Setting().ShowDialog();
			}
			//App.DB.ObjectTrackingEnabled = false;
		}

		void Current_Exit(object sender, ExitEventArgs e) {
			App.W.Quit(false, false, false);
		}
		private void _this_Loaded(object sender, RoutedEventArgs e) {
			e.Handled = true;
			cbxGBUser.ItemsSource = App.DB.Employees.OrderBy(x=>x.Nick);
			GBUser = App.DB.Employees.Where(x => x.ID == Properties.Settings.Default.GUID).Single();
			cbxGBUser.SelectedItem = GBUser;
			if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
				Title = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
			else
				Title = "";
			Title+=" ["+GBUser.Nick+"]";
		}
		private void mainTabs_SelectionChanged(object sender, SelectionChangedEventArgs e) {
			//if(!(e.Source is TabControl))return;
			//GC.Collect();
			//App.DB.Dispose();
			//App.DB = new m.DataClassesDataContext();
		}
		private void miSetting_Click(object sender, RoutedEventArgs e) {
			new Setting().ShowDialog();
		}
		private void _this_Closed(object sender, EventArgs e) {
			
		}

		private void miTemp_Click(object sender, RoutedEventArgs e) {
			System.IO.FileInfo fi=new System.IO.FileInfo("Temp");
			System.Diagnostics.Process.Start("explorer.exe", fi.FullName);
		}

		public static m.Employee GBUser { get; set; }

		private void cbxGBUser_SelectionChanged(object sender, SelectionChangedEventArgs e) {
			GBUser = cbxGBUser.SelectedItem as m.Employee;
			if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
				Title = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
			else
				Title = "";
			Title += " [" + GBUser.Nick + "]";
		}

		private void miReport_Click(object sender, RoutedEventArgs e) {
			new r.ReportChoseDate().ShowDialog();
		}
	}
}
